package com.snapan.service.impl;

import com.snapan.service.EmailService;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;


@Service
public class EmailServiceImpl implements EmailService {

    // 1. 调整mailSender的类型为JavaMailSenderImpl（与接口方法参数一致）
    @Autowired
    private JavaMailSenderImpl mailSender;  // 现在Spring会主动赋值，不再为null


    private String fromEmail = "1659301571@qq.com";

    // 2. 实现接口的setMailSender方法
    @Override
    public void setMailSender(JavaMailSenderImpl mailSender) {
        this.mailSender = mailSender;
    }

    // 3. 实现接口的setFromEmail方法
     @Value("${mail.smtp.username:}")  // 对应mail.properties中的配置，如果不存在则使用空字符串
    public void setFromEmail(String fromEmail) {
        this.fromEmail = fromEmail;
    }
    @Override
    public boolean sendVerificationCode(String toEmail, String verificationCode, Byte type) {
        try {
            String subject = getEmailSubject(type);
            String content = buildEmailContent(verificationCode, type);
            sendHtmlEmail(toEmail, subject, content);
            System.out.println("验证码邮件发送成功至: " + toEmail);
            return true;  // 发送成功返回true
        } catch (Exception e) {
            System.err.println("邮件发送失败: " + e.getMessage());
            e.printStackTrace();  // 打印详细堆栈，方便排查问题
            return false;  // 发送失败返回false
        }
    }

    // 以下方法保持不变
    private String getEmailSubject(Byte type) {
        switch (type) {
            case 1: return "Snapan - 注册验证码";
            case 2: return "Snapan - 密码重置验证码";
            default: return "Snapan - 验证码";
        }
    }

    // 邮件发送方法
    private void sendHtmlEmail(String toEmail, String subject, String htmlContent) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

        helper.setFrom(fromEmail);
        helper.setTo(toEmail);
        helper.setSubject(subject);
        helper.setText(htmlContent, true);

        mailSender.send(message);
    }

    private String buildEmailContent(String verificationCode, Byte type) {
        String title = type == 1 ? "邮箱验证" : "密码重置";
        String action = type == 1 ? "注册" : "重置密码";

        return "<!DOCTYPE html>" +
                "<html lang=\"zh-CN\">" +
                "<head>" +
                "    <meta charset=\"UTF-8\">" +
                "    <style>" +
                "        body { font-family: 'Microsoft YaHei', Arial, sans-serif; line-height: 1.6; color: #333; background: #f5f5f5; padding: 20px; }" +
                "        .container { max-width: 600px; margin: 0 auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }" +
                "        .header { text-align: center; margin-bottom: 30px; border-bottom: 1px solid #eee; padding-bottom: 20px; }" +
                "        .code { font-size: 32px; color: #1890ff; font-weight: bold; text-align: center; margin: 30px 0; padding: 20px; background: #f0f8ff; border-radius: 6px; letter-spacing: 8px; }" +
                "        .tip { color: #666; margin: 15px 0; line-height: 1.8; }" +
                "        .footer { margin-top: 40px; padding-top: 20px; border-top: 1px solid #eee; color: #999; font-size: 12px; text-align: center; }" +
                "        .warning { color: #ff4d4f; font-weight: bold; }" +
                "    </style>" +
                "</head>" +
                "<body>" +
                "    <div class=\"container\">" +
                "        <div class=\"header\">" +
                "            <h2>Snapan " + title + "</h2>" +
                "        </div>" +
                "        <p>您好！</p>" +
                "        <p>您正在申请" + action + " Snapan 服务，您的验证码是：</p>" +
                "        <div class=\"code\">" + verificationCode + "</div>" +
                "        <p class=\"tip\">• 验证码有效期为 <span class=\"warning\">10 分钟</span></p>" +
                "        <p class=\"tip\">• 请尽快完成操作</p>" +
                "        <p class=\"tip\">• 如果这不是您本人的操作，请忽略此邮件</p>" +
                "        <div class=\"footer\">" +
                "            <p>Snapan 团队</p>" +
                "            <p>" + new java.util.Date() + "</p>" +
                "        </div>" +
                "    </div>" +
                "</body>" +
                "</html>";
    }
}